package com.os.view;

import com.os.callback.FileNameCallback;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.concurrent.CountDownLatch;

/**
 * @author ZZH-LDX
 * @version: 1.0
 * 创建文件/文件夹名称的视图
 * name.txt 表示文件
 * $name 表示文件夹
 */
public class CreateNameView {

	private Stage stage;
	private Scene scene;
	private HBox hBox;
	private TextField nameField;
	private Button okButton, cancelButton;
	private boolean isFolder;// 是否是文件夹true 、 是否是文件false

	private String fileSuffix = ".txt";// 文件后缀名

	private String currentName;

	private FileNameCallback callback;

	public void setFileNameCallback(FileNameCallback callback) {
		this.callback = callback;
	}


	public CreateNameView(boolean isFolder) {
		this.isFolder = isFolder;
		showView();
	}


	public String getFileName() throws InterruptedException {
		if (!isFolder){
			return currentName + fileSuffix;
		}else {
			return currentName;
		}
	}


	private void showView() {
		nameField = new TextField();
		nameField.setPromptText("请输入文件/文件夹名称");
		nameField.setPrefWidth(250);
		
		okButton = new Button("确定");
		okButton.setStyle("-fx-background-color:#e7e8ff;");
		okButton.setOnMouseEntered(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				okButton.setStyle("-fx-background-color: #808080;");
			}
		});
		okButton.setOnMouseExited(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				okButton.setStyle("-fx-background-color: #e7e8ff;");
			}
		});
		okButton.setOnAction(ActionEvent -> {
			currentName = nameField.getText();//更新
			stage.close();
			if (callback!= null) {
                try {
                    callback.onFileNameObtained(getFileName());
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
		});
		
		cancelButton = new Button("取消");
		cancelButton.setStyle("-fx-background-color:#c3fbe3;");
		cancelButton.setOnMouseEntered(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent event) {
				cancelButton.setStyle("-fx-background-color: #808080;");
			}
		});
		cancelButton.setOnMouseExited(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent event) {
				cancelButton.setStyle("-fx-background-color: #c3fbe3;");
			}
		});
		cancelButton.setOnAction(ActionEvent -> {
			stage.close();
		});
		
		Label label = new Label("输入新的文件/文件夹名称");
		label.setStyle("-fx-font-size:18px;-fx-font-weight:bold;");


		hBox = new HBox(okButton, cancelButton);
		hBox.setSpacing(8);
		hBox.setPadding(new Insets(5));
		hBox.setStyle("-fx-background-color:#ffffff;");
		hBox.setAlignment(Pos.CENTER);

		// 选择文件的后缀名
		ComboBox<String> suffixBox = new ComboBox<>();
		// 背景色修改
		suffixBox.setStyle("-fx-background-color:#f9ecec;");
		if (!isFolder){
			suffixBox.getItems().addAll(".txt",".jpg",".png" ,".pdf", ".doc", ".docx", ".xlsx");
			suffixBox.setValue(".txt");
			// 监听选择的后缀名
			suffixBox.valueProperty().addListener((observable, oldValue, newValue) -> {
				fileSuffix = newValue;
			});
		}else {// 文件夹
			suffixBox.setStyle("-fx-background-color:#f9ecec;");
			suffixBox.getItems().add("目录");
			suffixBox.setValue("目录");
			fileSuffix = "";
		}
		// 放置到VBox中
		hBox.getChildren().add(suffixBox);

		HBox nameHbox = new HBox(nameField);
		nameHbox.setStyle("-fx-background-color:#ffffff;");
		nameHbox.setAlignment(Pos.CENTER);

		VBox vBox = new VBox(label,nameHbox, hBox);
		vBox.setStyle("-fx-background-color:#ffffff;");
		vBox.setSpacing(20);
		vBox.setAlignment(Pos.CENTER);
		scene = new Scene(vBox,360,200);
		stage = new Stage();
		stage.setScene(scene);
//		stage.initStyle(StageStyle.UNDECORATED);
		stage.setAlwaysOnTop(true);
		stage.show();
	}	


}
